package com.meihua.shellapp.utils;

import android.view.View;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.lang.reflect.ParameterizedType;

/**
 * 作者：lc on 2017/10/14.
 * 邮箱：lc0607live@163.com
 */

public class MyStringCallBack<T> extends StringCallback {
    private T t;
    private View v;
    private IRecyclerView rv;

    public MyStringCallBack(View v) {
        this.v = v;
    }

    public MyStringCallBack(IRecyclerView rv) {
        this.rv = rv;
    }

    @Override
    public void onStart(Request<String, ? extends Request> request) {
        super.onStart(request);
        if (v != null) {
            v.setEnabled(false);
        }
    }


    @Override
    public void onSuccess(Response<String> response) {
        if (rv != null) {
            rv.setRefreshing(false);
            ((LoadMoreFooterView) rv.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.GONE);
        } else if (v != null) {
            v.setEnabled(true);
        }
        t = MyApplication.getInstance().
                getBeanForJson((Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0],
                        response.body());
        if (t == null) {
            return;
        }
        onSuccessData(t);
    }


    public void onSuccessData(T bean) {

    }

    @Override
    public void onError(Response<String> response) {
        super.onError(response);
        if (rv != null) {
            rv.setRefreshing(false);
            ((LoadMoreFooterView) rv.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.GONE);
        } else if (v != null) {
            v.setEnabled(true);
        }
    }
}
